// pages/collection-list/collection-list.js
const app = getApp();
const request = require('../../../utils/request.js');
const config = require('../../../utils/config');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    TabCur: 0,
    isResult: true,
    collectionList: [],
    pageNum: 1,
    pageSize: 10,
    params: {}
  },

  /**
   * 切换标签
   */
  tabSelect(e) {
    this.setData({
      TabCur: e.currentTarget.dataset.id,
      scrollLeft: (e.currentTarget.dataset.id-1)*60,
      collectionList: [],
      pageNum: 1
    });
    this.getCollectionList(this.data.pageNum, this.data.pageSize);
  },

  /**
   * 获取我的收藏列表
   */
  getCollectionList(pageNum, pageSize){
    let that = this;
    let url = config.SERVER_URL;
    let token = wx.getStorageSync("token");
    if(this.data.TabCur == 0){
      url += "post/list/self/collections?pageNum=" + pageNum + "&pageSize=" + pageSize;
    }else{
      url += "activity/list/self/collections?pageNum=" + pageNum + "&pageSize=" + pageSize;
    }
    wx.showLoading({
      title: '加载ing',
    })
    request.requestGetApi(url, token, {}, "application/json").then(res=>{
      if(res.status === config.STATUS_OK){
        let isResult = res.data.rows.length >= pageSize;
        this.setData({
          isResult,
          collectionList: that.data.collectionList.concat(res.data.rows)
        });
      }
    }).catch(err=>{
      app.failFun(err);
    }).finally((res) => {
      app.completeFun(res);
    });
  },

  /**
   * 取消收藏活动
   */
  cancelPost(e){
    let that = this;
    let index = e.currentTarget.dataset.index;
    let token = wx.getStorageSync('token');
    let url = config.SERVER_URL;
    let collectionList = this.data.collectionList;
    url += 'post/cancelCollect/' + collectionList[index].id;
    collectionList.splice(index, 1);
    request.requestPostApi(url, token, {}, 'application/json').then(res=>{
      if(res.status === config.STATUS_OK){
        that.setData({
          collectionList
        });
        wx.showToast({
          title: res.message,
          icon: 'success',
          duration: 1500,
        });
      }
    }).catch(err=>{
      app.failFun(err);
    })
  },

  /**
   * 取消收藏活动
   */
  cancelActivity(e){
    let that = this;
    let index = e.currentTarget.dataset.index;
    let token = wx.getStorageSync('token');
    let url = config.SERVER_URL;
    let collectionList = this.data.collectionList;
    url += 'activity/collect_cancel/' + collectionList[index].id;
    collectionList.splice(index, 1);
    request.requestPostApi(url, token, {}, 'application/json').then(res=>{
      if(res.status === config.STATUS_OK){
        that.setData({
          collectionList
        });
        wx.showToast({
          title: res.message,
          icon: 'success',
          duration: 1500,
        });
      }
    }).catch(err=>{
      app.failFun(err);
    })
  },

  /**
   * 跳转到帖子详情页
   */
  toPostInfo(e){
    wx.navigateTo({
      url: '/pages/post/post?postId=' + e.currentTarget.dataset.postid
    });
  },
  
  /**
   * 跳转到活动详情页面
   */
  toActivityInfo(e){
    wx.navigateTo({
      url: '/pages/activity-info/activity-info?id=' + e.currentTarget.dataset.id
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getCollectionList(this.data.pageNum, this.data.pageSize);
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if(!this.data.isResult){
      return;
    }
    this.setData({
      pageNum: this.data.pageNum + 1
    });
    this.getCollectionList(this.data.pageNum, this.data.pageSize);
  },

  // ListTouch触摸开始
  ListTouchStart(e) {
    this.setData({
      ListTouchStart: e.touches[0].pageX
    })
  },

  // ListTouch计算方向
  ListTouchMove(e) {
    this.setData({
      ListTouchDirection: e.touches[0].pageX - this.data.ListTouchStart > 0 ? 'right' : 'left'
    })
  },

  // ListTouch计算滚动
  ListTouchEnd(e) {
    if (this.data.ListTouchDirection =='left'){
      this.setData({
        modalName: e.currentTarget.dataset.target
      })
    } else {
      this.setData({
        modalName: null
      })
    }
    this.setData({
      ListTouchDirection: null
    })
  },
})